import 'dart:developer';

import 'package:package_info_plus/package_info_plus.dart';
import 'package:sports_new/utils/app_config.dart';
import 'package:sports_new/utils/utils.dart';

import '../entity/other/resource_entity.dart';
import '../server/api/other_api.dart';

class ModeConfig{
  static bool expert = true;
  static bool expertRecord = true;
  static bool ai = true;
  static bool aiRecord = true;
  static bool viewpointOdds = true;
  static bool matchListOdds = true;
  static bool matchDetailOdds = true;
  static bool secretOdds = true;
  static bool planForm = true;
  static bool record = true;
  static bool vipCenter = true;
  static bool buttonArea = true;
  static bool gameEntry = true;
  static bool matchTabs = true;
  static bool _inAudit = false;

  static init() async{
    await checkAudit();
    if(_inAudit){
      expert = false;
      expertRecord = false;
      ai = false;
      aiRecord = false;
      viewpointOdds = false;
      matchListOdds = false;
      matchDetailOdds = false;
      secretOdds = false;
      planForm = false;
      record = false;
      vipCenter = false;
      buttonArea = false;
      gameEntry = false;
      matchTabs = false;
      return;
    }
    List<ResourceEntity>? config = await OtherApi.getAppList("mode_config");
    config?.forEach((element) {
      switch(element.button){
        case "expert":
          expert = element.content == "1";
          break;
        case "expert_record":
          expertRecord = element.content == "1";
          break;
        case "ai":
          ai = element.content == "1";
          break;
        case "ai_record":
          aiRecord = element.content == "1";
          break;
        case "plan_form":
          planForm = element.content == "1";
          break;
        case "viewpoint_odds":
          viewpointOdds = element.content == "1";
          break;
        case "match_list_odds":
          matchListOdds = element.content == "1";
          break;
        case "match_detail_odds":
          matchDetailOdds = element.content == "1";
          break;
        case "secret_odds":
          secretOdds = element.content == "1";
          break;
        case "odds":
          if(element.content == "0") {
            matchDetailOdds = false;
            matchListOdds = false;
            secretOdds = false;
          }
      }
    });
    var odds = config?.where(
      (element) => element.button == "odds"
    ).toList();
    if(odds.hasValue) {
      if(odds?.first.content == "0") {
        viewpointOdds = false;
        matchListOdds = false;
        matchDetailOdds = false;
        secretOdds = false;
      }
    }
  }

  static Future checkAudit() async{
    List<ResourceEntity>? channelAudit = await OtherApi.getAppList("channel_audit_config");
    ResourceEntity? audit;
    try{
      audit = channelAudit?.where((e) => e.button == AppConfig.config.channel).first;
    }catch(e){
      log("checkAudit error:$e");
    }
    if(audit != null){
      final package = await PackageInfo.fromPlatform();
      if(audit.content?.split(",").contains(package.version)??false){
        _inAudit = true;
      }
    }
  }
}